import tkinter as tk
from tkinter import messagebox


class ActivationDialog:
    def __init__(self, parent, license_manager):
        self.window = tk.Toplevel(parent)
        self.window.title("激活程序")
        self.license_manager = license_manager

        # 设置窗口大小和位置
        window_width = 400
        window_height = 200
        screen_width = self.window.winfo_screenwidth()
        screen_height = self.window.winfo_screenheight()
        x = (screen_width - window_width) // 2
        y = (screen_height - window_height) // 2
        self.window.geometry(f"{window_width}x{window_height}+{x}+{y}")

        # 机器码显示
        machine_id_frame = tk.Frame(self.window)
        machine_id_frame.pack(pady=10)
        tk.Label(machine_id_frame, text="机器码：").pack(side=tk.LEFT)
        machine_id = tk.Entry(machine_id_frame, width=40)
        machine_id.insert(0, license_manager.config['machine_id'])
        machine_id.config(state='readonly')
        machine_id.pack(side=tk.LEFT)

        # License输入
        license_frame = tk.Frame(self.window)
        license_frame.pack(pady=10)
        tk.Label(license_frame, text="License：").pack(side=tk.LEFT)
        self.license_entry = tk.Entry(license_frame, width=40)
        self.license_entry.pack(side=tk.LEFT)

        # 按钮
        button_frame = tk.Frame(self.window)
        button_frame.pack(pady=20)
        tk.Button(button_frame, text="激活", command=self.activate).pack(side=tk.LEFT, padx=10)
        tk.Button(button_frame, text="取消", command=self.window.destroy).pack(side=tk.LEFT, padx=10)

        self.window.grab_set()

    def activate(self):
        license_key = self.license_entry.get().strip()
        if not license_key:
            messagebox.showerror("错误", "请输入License Key")
            return

        success, message = self.license_manager.activate_license(license_key)
        if success:
            messagebox.showinfo("成功", message)
            self.window.destroy()
        else:
            messagebox.showerror("错误", message)